package com.zee.demo;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class ListWithEditTextDialog extends Dialog {

    private final Context mContext;
    private final List<String> mItems;
    private final OnConfirmListener mListener;
    private final String mTitle;
    private final String mEditHint;

    public interface OnConfirmListener {
        void onConfirm(String inputText);
        void onItemSelected(int position, String text);
    }

    public ListWithEditTextDialog(@NonNull Context context, String title, String editHint,
                                  List<String> items, OnConfirmListener listener) {
        super(context, R.style.CustomDialogStyle);
        mContext = context;
        mTitle = title;
        mEditHint = editHint;
        mItems = items;
        mListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_list_with_edittext);

        // 设置对话框宽度
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
        }

        // 初始化视图
        TextView titleTv = findViewById(R.id.dialog_title);
        ListView listView = findViewById(R.id.list_view);
        EditText editText = findViewById(R.id.edit_text);
        Button cancelBtn = findViewById(R.id.btn_cancel);
        Button confirmBtn = findViewById(R.id.btn_confirm);

        // 设置标题
        titleTv.setText(mTitle);
        // 设置编辑框提示
        editText.setHint(mEditHint);

        // 初始化列表适配器
        final ItemAdapter adapter = new ItemAdapter(mContext,
                android.R.layout.simple_list_item_1, mItems);
        listView.setAdapter(adapter);

        // 列表项点击事件
        listView.setOnItemClickListener((parent, view, position, id) -> {
                    if (mListener != null) {
                        mListener.onItemSelected(position, mItems.get(position));
                    }
            dismiss();
        });

        // 取消按钮
        cancelBtn.setOnClickListener(v -> dismiss());

        // 确认按钮
        confirmBtn.setOnClickListener(v -> {
            String inputText = editText.getText().toString().trim();
            if (!inputText.isEmpty()) {
                mItems.add(inputText);
                adapter.notifyDataSetChanged();
                editText.setText("");
            }
        });
    }

    // 自定义适配器，用于显示选中状态
    private class ItemAdapter extends ArrayAdapter<String> {

        public ItemAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
            super(context, resource, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = view.findViewById(android.R.id.text1);

            textView.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
            textView.setTextColor(mContext.getResources().getColor(android.R.color.black));

            return view;
        }
    }
}

